#pragma once

#include <leveldb/env.h>
#include <leveldb/slice.h>

#include <ace/Service_Object.h>
#include <ace/Task.h>

#if defined(DeleteFile)
#undef DeleteFile
#endif

class MyBGThread :  public ACE_Task<ACE_MT_SYNCH>{
public:
  virtual int svc (void);  
};

class MyDbEnv : public leveldb::Env,public ACE_Service_Object {
public:
	/// Initializes object when dynamic linking occurs.
	virtual int init (int argc, ACE_TCHAR *argv[]);

	/// Terminates object when dynamic unlinking occurs.
	virtual int fini (void);

	MyDbEnv();
	virtual ~MyDbEnv() ;

	virtual leveldb::Status NewSequentialFile(const std::string& fname,leveldb::SequentialFile** result) ;

	virtual leveldb::Status NewRandomAccessFile(const std::string& fname,leveldb::RandomAccessFile** result) ;

	virtual leveldb::Status NewWritableFile(const std::string& fname,leveldb::WritableFile** result) ;

	virtual bool FileExists(const std::string& fname) ;

	virtual leveldb::Status GetChildren(const std::string& dir,std::vector<std::string>* result) ;

	virtual leveldb::Status DeleteFile(const std::string& fname) ;

	virtual leveldb::Status CreateDir(const std::string& name) ;

	virtual leveldb::Status DeleteDir(const std::string& name) ;

	virtual leveldb::Status GetFileSize(const std::string& fname, uint64_t* size) ;

	virtual leveldb::Status RenameFile(const std::string& src, const std::string& target) ;

	virtual leveldb::Status LockFile(const std::string& fname, leveldb::FileLock** lock) ;

	virtual leveldb::Status UnlockFile(leveldb::FileLock* lock) ;

	virtual void Schedule(void (*function)(void*), void* arg);

	virtual void StartThread(void (*function)(void* arg), void* arg);

	virtual leveldb::Status GetTestDirectory(std::string* result);
		
	virtual leveldb::Status NewLogger(const std::string& fname, leveldb::Logger** result) ;

	virtual uint64_t NowMicros() ;

	virtual void SleepForMicroseconds(int micros) ;

private:					
	size_t page_size_;			
	MyBGThread bgthread_;			
};

